package com.siecom.transport.handler;

import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.siecom.bean.BankCardInfoBean;
import com.siecom.bean.TradeLogBean;
import com.siecom.constconfig.ConstDefine;
import com.siecom.constconfig.ErrorDefine;
import com.siecom.hostservice.MainActivity;
import com.siecom.module.DeviceCallback;
import com.siecom.module.DeviceInterface;
import com.siecom.module.EMVParam;
import com.siecom.module.EmvOptions;
import com.siecom.proto.Message;
import com.siecom.tool.ByteTool;
import com.siecom.tool.LogUtil;
import com.siecom.transport.CommServer;
import com.siecom.transport.bluetooth.BlueServer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;


public class FinanHandler implements ServiceHandler {
    private static final String TAG = FinanHandler.class.getSimpleName();
    private CommServer server;
    private String tempMsgId = null;
    @Override
    public void onReceive(CommServer server, Message.siecomproto msg,String message) {
        this.server = server;
        Log.e(TAG, "onReceive----" + msg.getCmd());
        disposeMessage(msg);

    }

    public void disposeMessage(Message.siecomproto msg) {


        switch (msg.getCmd()) {
            case ConstDefine.IC_LOG_REQUEST:
            case ConstDefine.PIC_LOG_REQUEST:
            case ConstDefine.AUTO_LOG_REQUEST:
                readLog(msg);
                break;

            case ConstDefine.PIC_LOAD_REQUEST:
            case ConstDefine.IC_LOAD_REQUEST:
            case ConstDefine.AUTO_LOAD_REQUEST:
                readLoadLog(msg);
                break;

            case ConstDefine.ARQC_REQUEST:
                ARPCExecute(msg);
                break;
            case ConstDefine.CANCEL_FINAN_REQUEST:
                cancelTask(msg);
                break;

            case ConstDefine.ICCard_POWER_ON_REQUEST:
                icCardPowerOn(msg,1); // 1 on  0 off
                break;
            case ConstDefine.ICCard_POWER_OFF_REQUEST:
                icCardPowerOn(msg,0);
                break;
            case ConstDefine.APDU_COMMAND_REQUEST:
                icCardApdu(msg);
                break;
            case ConstDefine.READ_SOCIAL_CARD_REQUEST: //读社保卡
                readSocialCard(msg);
                break;
            case ConstDefine.AUTO_CHECK_REQUEST:
                autoReadCheckCard(msg);
                break;
            default:
                readCardInfo(msg);
                break;
        }


    }

    public void readSocialCard(Message.siecomproto msg){
        DeviceInterface.readSocialCard(new DeviceCallback() {
            @Override
            public void onStart() {
                MainActivity.getmSys().DLL_SysLedControl((byte) 2,(byte)1);
                MainActivity.startInsideCard(2,msg.getTimeout());
            }

            @Override
            public void onError(Bundle bundle) {
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.FINAN_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setTimeout(msg.getTimeout());
                msgBuilder.setRespond(bundle.getString("message"));
                msgBuilder.setError(bundle.getInt("code"));
                com.siecom.proto.Message.siecomproto msg = msgBuilder.build();
                server.sendToRemote(msg);
                MainActivity.stopInsideCard();
                MainActivity.playVoice(ConstDefine.OPERATE_FAIL);
            }

            @Override
            public void onSuccess(Bundle bundle) {
                String respond = bundle.getString(ConstDefine.data,"");
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.FINAN_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setTimeout(msg.getTimeout());
                msgBuilder.setRespond(respond);
                msgBuilder.setError(0);
                com.siecom.proto.Message.siecomproto msg = msgBuilder.build();
                server.sendToRemote(msg);
            }

            @Override
            public void onCompleted(Bundle bundle) {
                MainActivity.getmSys().DLL_SysLedControl((byte) 2,(byte)0);
                MainActivity.stopInsideCard();
            }
        },msg.getTimeout(),false);
    }

    //IC卡上电
    public void icCardPowerOn(Message.siecomproto msg,int onOff){
        tempMsgId = msg.getMsgid();
        int cardType ;
        try {
            JSONObject jb = new JSONObject(msg.getRequest());
            cardType = jb.optInt("cardType");
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        DeviceCallback callback = new DeviceCallback() {
            @Override
            public void onStart() {

                if (cardType == 0){
                    MainActivity.getmSys().DLL_SysLedControl((byte) 2,(byte)1);
                    MainActivity.startInsideCard(3,msg.getTimeout());
                }else if (cardType == 1){
                    MainActivity.getmSys().DLL_SysLedControl((byte) 3,(byte)1);
                    MainActivity.startNfcCard(3,msg.getTimeout());
                }else {
                    MainActivity.getmSys().DLL_SysLedControl((byte) 2,(byte)1);
                    MainActivity.getmSys().DLL_SysLedControl((byte) 3,(byte)1);
                    MainActivity.startInoneCard(3,msg.getTimeout());
                }


            }

            @Override
            public void onError(Bundle bundle) {
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.FINAN_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setTimeout(msg.getTimeout());
                msgBuilder.setRespond(bundle.getString("message"));
                msgBuilder.setError(bundle.getInt("code"));
                com.siecom.proto.Message.siecomproto msg = msgBuilder.build();
                server.sendToRemote(msg);
            }

            @Override
            public void onSuccess(Bundle bundle) {
                String respond = bundle.getString(ConstDefine.data,"");
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.FINAN_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setTimeout(msg.getTimeout());
                msgBuilder.setRespond(respond);
                msgBuilder.setError(0);
                com.siecom.proto.Message.siecomproto msg = msgBuilder.build();
                server.sendToRemote(msg);
            }

            @Override
            public void onCompleted(Bundle bundle) {
                if (onOff == 0){
                    MainActivity.stopInoneCard();
                    MainActivity.stopInsideCard();
                    MainActivity.stopNfcCard();
                }
            }
        };
        if (onOff == 1)
            DeviceInterface.icCardPowerOn(callback,null,msg.getTimeout(),false);
        if (onOff == 0)
            DeviceInterface.icCardPowerOff(callback,null,msg.getTimeout(),false);
    }

    public void icCardApdu(Message.siecomproto msg){
        DeviceCallback callback = new DeviceCallback() {
            @Override
            public void onStart() {

            }

            @Override
            public void onError(Bundle bundle) {
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.FINAN_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setTimeout(msg.getTimeout());
                msgBuilder.setRespond(bundle.getString("message"));
                msgBuilder.setError(bundle.getInt("code"));
                com.siecom.proto.Message.siecomproto msg = msgBuilder.build();
                server.sendToRemote(msg);
            }

            @Override
            public void onSuccess(Bundle bundle) {
                String respond = bundle.getString(ConstDefine.data);
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.FINAN_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setTimeout(msg.getTimeout());
                msgBuilder.setRespond(respond);
                msgBuilder.setError(0);
                com.siecom.proto.Message.siecomproto msg = msgBuilder.build();
                server.sendToRemote(msg);
            }

            @Override
            public void onCompleted(Bundle bundle) {

            }
        };
        int cardType = -1;
        String apduSend = "";
        try {
            JSONObject jb = new JSONObject(msg.getRequest());
            apduSend = jb.optString("apdu");
            cardType = jb.optInt("cardType");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        DeviceInterface.icCardApdu(callback,apduSend,msg.getTimeout(),false);
    }


    public void readLoadLog(Message.siecomproto msg) {

        DeviceCallback callback = new DeviceCallback() {
            @Override
            public void onStart() {


            }

            @Override
            public void onError(Bundle bundle) {

                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.FINAN_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setTimeout(msg.getTimeout());
                msgBuilder.setRespond(bundle.getString("message"));
                msgBuilder.setError(bundle.getInt("code"));
                com.siecom.proto.Message.siecomproto msg = msgBuilder.build();
                server.sendToRemote(msg);


            }

            @Override
            public void onSuccess(Bundle bundle) {
                List<TradeLogBean> list = bundle.getParcelableArrayList("data");
                Gson gson = new Gson();
                String respond = gson.toJson(list);
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.FINAN_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setTimeout(msg.getTimeout());
                msgBuilder.setRespond(respond);
                msgBuilder.setError(0);
                com.siecom.proto.Message.siecomproto msg = msgBuilder.build();
                server.sendToRemote(msg);

            }

            @Override
            public void onCompleted(Bundle bundle) {


            }
        };
        if (msg.getCmd() == ConstDefine.IC_LOAD_REQUEST) {
            DeviceInterface.readIcLoadLog(callback, null, msg.getTimeout(), false);
        } else if (msg.getCmd() == ConstDefine.PIC_LOAD_REQUEST) {
            DeviceInterface.readPicLoadLog(callback, null, msg.getTimeout(), false);
        } else if  (msg.getCmd() == ConstDefine.AUTO_LOAD_REQUEST) {
            DeviceInterface.readLoadLog(callback, null, msg.getTimeout(), false);
        }

    }

    public void cancelTask(Message.siecomproto msg){

        DeviceInterface.cancelFinancial();
        com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
        msgBuilder.setModule(ConstDefine.FINAN_MODULE);
        msgBuilder.setMsgid(msg.getMsgid());
        msgBuilder.setCmd(msg.getCmd() + 1);
        msgBuilder.setRespond("Finan cancel");
        msgBuilder.setError(0);
        com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
        server.sendToRemote(respondMsg);

        if (tempMsgId != null){
            msgBuilder.setModule(ConstDefine.FINAN_MODULE);
            msgBuilder.setMsgid(tempMsgId);
            msgBuilder.setCmd(ConstDefine.IC_RESPOND);
            msgBuilder.setRespond("Finan cancel");
            msgBuilder.setError(ErrorDefine.FINAN_OPERATE_CANCEL);
            com.siecom.proto.Message.siecomproto respondMsg2 = msgBuilder.build();
            server.sendToRemote(respondMsg2);
        }

    }

    public void ARPCExecute(Message.siecomproto msg) {
        JSONObject json = null;
        int cardType = 0;
        String arpc = null;
        String script = null;
        try {
            json = new JSONObject(msg.getRequest());
            cardType = json.getInt("cardType");
            arpc = json.getString("arpc");
            script = json.getString("script");
        } catch (JSONException e) {
            e.printStackTrace();

        }
        DeviceInterface.ARPCExecuteScript(cardType, arpc, script, new DeviceCallback() {
            @Override
            public void onStart() {

            }

            @Override
            public void onError(Bundle bundle) {
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.FINAN_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setRespond(bundle.getString("message"));
                msgBuilder.setError(bundle.getInt("code"));
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);
            }

            @Override
            public void onSuccess(Bundle bundle) {
                JSONObject json = new JSONObject();
                try {
                    json.put("result", bundle.getInt("result"));
                    json.put("ScriptResult", bundle.getString("ScriptResult"));
                    json.put("TC", bundle.getString("TC"));
                }catch (JSONException e){

                }
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.FINAN_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setError(0);
                msgBuilder.setRespond(json.toString());
                com.siecom.proto.Message.siecomproto respondMsg = msgBuilder.build();
                server.sendToRemote(respondMsg);
            }

            @Override
            public void onCompleted(Bundle bundle) {


            }

        },msg.getTimeout(),false);
    }


    /**
     * 读交易日志
     * @param msg
     */
    public void readLog(Message.siecomproto msg) {

        DeviceCallback callback = new DeviceCallback() {
            @Override
            public void onStart() {


            }

            @Override
            public void onError(Bundle bundle) {

                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.FINAN_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setTimeout(msg.getTimeout());
                msgBuilder.setRespond(bundle.getString("message"));
                msgBuilder.setError(bundle.getInt("code"));
                com.siecom.proto.Message.siecomproto msg = msgBuilder.build();
                server.sendToRemote(msg);


            }

            @Override
            public void onSuccess(Bundle bundle) {
                Log.e("readLog","onSuccess");
                List<TradeLogBean> list = bundle.getParcelableArrayList("data");
                Gson gson = new Gson();
                String respond = gson.toJson(list);
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.FINAN_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setTimeout(msg.getTimeout());
                msgBuilder.setRespond(respond);
                msgBuilder.setError(0);
                com.siecom.proto.Message.siecomproto msg = msgBuilder.build();
                server.sendToRemote(msg);

            }

            @Override
            public void onCompleted(Bundle bundle) {


            }
        };
        if (msg.getCmd() == ConstDefine.IC_LOG_REQUEST) {
            DeviceInterface.readIcLog(callback, null, msg.getTimeout(), false);
        } else if (msg.getCmd() == ConstDefine.PIC_LOG_REQUEST) {
            DeviceInterface.readPicLog(callback, null, msg.getTimeout(), false);
        }else if (msg.getCmd() == ConstDefine.AUTO_LOG_REQUEST) {
            DeviceInterface.autoReadLog(callback, null, msg.getTimeout(), false);
        }

    }

    public void autoReadCheckCard(Message.siecomproto msg){
        DeviceInterface.autoCardCheckCard(new DeviceCallback() {
            @Override
            public void onStart() {
                MainActivity.getmSys().DLL_SysLedControl((byte) 2,(byte)1);
                MainActivity.getmSys().DLL_SysLedControl((byte) 3,(byte)1);
                MainActivity.startInoneCard(0,msg.getTimeout());
            }

            @Override
            public void onError(Bundle bundle) {
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.FINAN_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setTimeout(msg.getTimeout());
                msgBuilder.setRespond(bundle.getString("message"));
                msgBuilder.setError(bundle.getInt("code"));
                com.siecom.proto.Message.siecomproto msg = msgBuilder.build();
                server.sendToRemote(msg);

                MainActivity.playVoice(ConstDefine.OPERATE_FAIL);
            }

            @Override
            public void onSuccess(Bundle bundle) {
                int slot = bundle.getInt("slot");
                String respond = slot + "";
                if (slot == 0){ //磁条卡
                    BankCardInfoBean bean = bundle.getParcelable("data");
                    Gson gson = new Gson();
                    respond = gson.toJson(bean);
                    MainActivity.playVoice(ConstDefine.OPERATE_SUCC);

                }

                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setModule(ConstDefine.FINAN_MODULE);
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setTimeout(msg.getTimeout());
                msgBuilder.setRespond(respond);
                msgBuilder.setError(0);
                com.siecom.proto.Message.siecomproto msg = msgBuilder.build();
                server.sendToRemote(msg);

                MainActivity.getmSys().DLL_SysLedControl((byte)2,(byte)0);
                MainActivity.getmSys().DLL_SysLedControl((byte)3,(byte)0);
                MainActivity.stopInoneCard();

            }

            @Override
            public void onCompleted(Bundle bundle) {

            }
        },null,null,msg.getTimeout(),false);
    }
    public void readCardInfo(Message.siecomproto msg) {
        tempMsgId = msg.getMsgid();
        EmvOptions options = new EmvOptions();
        EMVParam emvParam = new EMVParam();
//        emvParam.parseFromBytes(msg.getExtion().toByteArray());
        options.setEmvParam(null);
        String[]  wantTags = null;
        int voiceType = 0;
        try {
            JSONObject jsonObject = new JSONObject(msg.getRequest());
            if(jsonObject.has("wantTags")) {
                JSONArray jsonArray = jsonObject.getJSONArray("wantTags");
                wantTags = new String[jsonArray.length()];
                for (int i = 0; i < jsonArray.length(); i++) {
                    wantTags[i] = jsonArray.getString(i);
                }
            }
            voiceType = jsonObject.optInt("voiceType");
            LogUtil.e(TAG,"voiceType == " + voiceType);
            options.setTime(jsonObject.getString("time"));
            options.setTransNo(jsonObject.getInt("TransNo"));
            options.setAuthorizedAmount(jsonObject.getInt("authorizedAmount"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        int finalVoiceType = voiceType;
        LogUtil.e(TAG,"finalVoiceType == " + finalVoiceType);
        DeviceCallback callback = new DeviceCallback() {
            @Override
            public void onStart() {

                switch (msg.getCmd()) {
                    case ConstDefine.IC_REQUEST:
                        MainActivity.getmSys().DLL_SysLedControl((byte) 2,(byte)1);
                        MainActivity.startInsideCard(finalVoiceType,msg.getTimeout());
                        break;
                    case ConstDefine.PIC_REQUEST:
                        MainActivity.startNfcCard(finalVoiceType,msg.getTimeout());
                        break;
                    case ConstDefine.MSR_REQUEST:
                        MainActivity.getmSys().DLL_SysLedControl((byte) 3,(byte)1);
                        MainActivity.startSwipeCard(msg.getTimeout());
                        break;
                    case ConstDefine.AUTO_REQUEST:
                        MainActivity.getmSys().DLL_SysLedControl((byte) 2,(byte)1);
                        MainActivity.getmSys().DLL_SysLedControl((byte) 3,(byte)1);
                        MainActivity.startInoneCard(finalVoiceType,msg.getTimeout());
                        break;

                }
            }

            @Override
            public void onError(Bundle bundle) {

                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setModule(ConstDefine.FINAN_MODULE);
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setTimeout(msg.getTimeout());
                msgBuilder.setRespond(bundle.getString("message"));
                msgBuilder.setError(bundle.getInt("code"));
                com.siecom.proto.Message.siecomproto msg = msgBuilder.build();
                server.sendToRemote(msg);

                MainActivity.playVoice(ConstDefine.OPERATE_FAIL);
            }

            @Override
            public void onSuccess(Bundle bundle) {
                BankCardInfoBean bean = bundle.getParcelable("data");
                Gson gson = new Gson();
                String respond = gson.toJson(bean);
                com.siecom.proto.Message.siecomproto.Builder msgBuilder = com.siecom.proto.Message.siecomproto.newBuilder();
                msgBuilder.setMsgid(msg.getMsgid());
                msgBuilder.setModule(ConstDefine.FINAN_MODULE);
                msgBuilder.setCmd(msg.getCmd() + 1);
                msgBuilder.setTimeout(msg.getTimeout());
                msgBuilder.setRespond(respond);
                msgBuilder.setError(0);
                com.siecom.proto.Message.siecomproto msg = msgBuilder.build();
                server.sendToRemote(msg);

                MainActivity.playVoice(ConstDefine.OPERATE_SUCC);

            }

            @Override
            public void onCompleted(Bundle bundle) {
                switch (msg.getCmd()) {
                    case ConstDefine.IC_REQUEST:
                        MainActivity.stopInsideCard();
                        break;
                    case ConstDefine.PIC_REQUEST:
                        MainActivity.stopNfcCard();
                        break;
                    case ConstDefine.MSR_REQUEST:
                        MainActivity.stopSwipeCard();
                        break;
                    case ConstDefine.AUTO_REQUEST:
                        MainActivity.stopInoneCard();
                        break;

                }
                MainActivity.getmSys().DLL_SysLedControl((byte)2,(byte)0);
                MainActivity.getmSys().DLL_SysLedControl((byte)3,(byte)0);
            }
        };
        Log.e(TAG, msg.getCmd() + "===");


        switch (msg.getCmd()) {
            case ConstDefine.IC_REQUEST:
                DeviceInterface.readIcInfo(callback, options,wantTags, msg.getTimeout(), false);
                break;
            case ConstDefine.PIC_REQUEST:
                DeviceInterface.readPiccInfo(callback, options, wantTags,msg.getTimeout(), false);
                break;
            case ConstDefine.MSR_REQUEST:
                DeviceInterface.readMsr(callback, options, msg.getTimeout(), false);
                break;
            case ConstDefine.AUTO_REQUEST:
                DeviceInterface.autoCardInfo(callback, options,wantTags, msg.getTimeout(), false);
                break;


        }

    }
}
